'use strict';

// Ionic Starter App

var ionicTea = angular.module('wb-tea', ['ionic','wb-tea.directives', 'angular-md5', 'wb-tea.controllers', 'wb-tea.services', 'wb-tea.friends.services', 'ngCordova', 'ngFileUpload','ionic-citypicker','ion-gallery']).run(function ($ionicPlatform, $rootScope, $ionicHistory, $state) {
    $ionicPlatform.ready(function () {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if (window.StatusBar) {
            // org.apache.cordova.statusbar required
            StatusBar.styleDefault();
        }

                 // enable back to last state
        $rootScope.$on('$stateChangeStart', function (event, to, toParams, from, fromParams) {
            if (from.name.indexOf('tab') != -1) {
              $state.previous = from;
            }
        });

    });
}).run(function($rootScope, $state, $stateParams, $ionicHistory, $ionicPopup, MsgService) {
    //back button function called from back button's ng-click="back()"
    $rootScope.back = function() {//实现返回的函数
    	// if ($ionicHistory.backView()){
    	// 	$ionicHistory.goBack();
    	// }else{
    	// 	$state.go($rootScope.previousState_name,$rootScope.previousState_params);
    	// }

        if ($ionicHistory.backView() != null) {
            $ionicHistory.goBack();
        }
        else {
            if ($state.previous != null) {
              $state.go($state.previous.name);
              $state.previous = null;
            }
        }

    };

    $state.backToLastView = function() {

      if ($ionicHistory.backView() != null) {
        $ionicHistory.goBack();
      } else {
        if ($state.previous != null) {
          $state.go($state.previous.name);
          $state.previous = null;
        }
      }
    };

    $rootScope.alert_suc = function(text) {
     	var alertPopup = $ionicPopup.alert({
       		title: '<p class="font-color-green">成功</p>',
       		template: '<p class="text-center font-color-gray">'+text+'</p>'
     	});
     	alertPopup.then(function(res) {
       		console.log('Thank you for not eating my delicious ice cream cone');
     	});
   	};

   	$rootScope.alert_err = function(text) {
     	var alertPopup = $ionicPopup.alert({
       		title: '<p class="font-color-xg">失败</p>',
       		template: '<p class="text-center font-color-xg">'+text+'</p>'
     	});
     	alertPopup.then(function(res) {
       		console.log('Thank you for not eating my delicious ice cream cone');
     	});
   	};

   	$rootScope.alert_msg = function(text) {
     	var alertPopup = $ionicPopup.alert({
       		title: "提示",
       		template: '<p class="text-center font-color-gray">'+text+'</p>'
     	});
     	alertPopup.then(function(res) {
       		console.log('Thank you for not eating my delicious ice cream cone');
     	});
   	};
}).config(function ($stateProvider, $urlRouterProvider, $ionicConfigProvider,ionGalleryConfigProvider,$sceDelegateProvider) {

    $sceDelegateProvider.resourceUrlWhitelist([
       // Allow same origin resource loads.
       'self',
       // Allow loading from our assets domain.  Notice the difference between * and **.
       'http://www.kuaidi100.com/**']);

	ionGalleryConfigProvider.setGalleryConfig({
          action_label: 'Close',
          toggle: false,
          fixed_row_size: true
  	});
    /*使用原生滑动，并没卵用的感觉*/
    if (!ionic.Platform.isIOS()) $ionicConfigProvider.scrolling.jsScrolling(false);

    $ionicConfigProvider.platform.ios.tabs.style('standard');

    $ionicConfigProvider.platform.ios.tabs.position('bottom');

    $ionicConfigProvider.platform.android.tabs.style('standard');

    $ionicConfigProvider.platform.android.tabs.position('standard');

    $ionicConfigProvider.platform.ios.navBar.alignTitle('center');

    $ionicConfigProvider.platform.android.navBar.alignTitle('left');

    $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');

    $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');

    $ionicConfigProvider.views.swipeBackEnabled(false);

    $ionicConfigProvider.platform.ios.views.transition('ios');

    $ionicConfigProvider.platform.android.views.transition('android');

    $stateProvider

    // setup an abstract state for the tabs directive
    .state('tab', {
        url: '/tab',
        abstract: true,
        templateUrl: 'templates/tabs.html'
    })

    // Each tab has its own nav history stack:

    .state('tab.main', {
        url: '/main',
        views: {
            'tab-main': {
                templateUrl: 'templates/tab-main.html',
                controller: 'DashCtrl'
            }
        }
    }).state('tab.notify', {
        url: '/notify',
        views: {
            'tab-msg': {
                templateUrl: 'templates/tab-notify.html',
                controller: 'notifyCtrl'
            }
        },
        cache: false
    }).state('notify-my-award', {
        url: "/notify/myaward",
        templateUrl: "templates/notify/my-award.html",
        controller: 'notifyCtrl'
    })
    // .state('create_comment', {
    //     url: "/product/comment",
    //     templateUrl: "templates/product/product-shop-comment.html",
    //     controller: 'productCommentCtrl',
    //     params: {
    //       id: "",
    //     }
    // })
    .state('create_comment', {
        url: "/product/comment",
        templateUrl: "templates/product/product-shop-comment.html",
        controller: 'productCtrl',
        params: {
          id: "",
        }
    })
    .state('notify-my-reply', {
        url: "/notify/reply",
        templateUrl: "templates/notify/my-reply.html",
        controller: 'notifyCtrl'
    }).state('notify-activity', {
        url: "/notify/activity",
        templateUrl: "templates/notify/activity-notify.html",
        controller: 'productCtrl'
    }).state('new-notify', {
        url: "/notify/new",
        templateUrl: "templates/notify/new-notify.html",
        controller: 'notifyCtrl'
    }).state('product-last', {
        url: "/product-last",
        templateUrl: "templates/product-last.html",
        controller: 'productCtrl',
        cache: 'false',
        params: { isSearch: null,product_search:null,keyword:''}
    }).state('product-tao', {
        url: "/product-tao",
        templateUrl: "templates/product-tao.html",
        controller: 'productCtrl'
    }).state('product-pick', {
        url: "/product-pick",
        templateUrl: "templates/product/product-pick.html",
        controller: 'productCtrl'
    }).state('product-info', {
        url: "/product-info",
        templateUrl: "templates/product/product-info.html",
        params: { "product_id": null },
        controller: 'productCtrl',
        cache: 'false'

    }).state('product-search', {
        url: "/product-search",
        templateUrl: "templates/product/product-search.html",
        params: { "product_list": null,'pick_param':null },
        controller: 'productCtrl'

    }).state('product-orderConfirm', {
        url: "/product-orderConfirm",
        templateUrl: "templates/product/product-orderConfirm.html",
        params: { "orderInfo": {}, "from_url": null },
        controller: 'productCtrl',
        cache: 'false'
    }).state('order-detail', {
        url: "/order/detail",
        templateUrl: "templates/account/order/order-detail.html",
        controller: 'orderCtrl',
        cache: 'false'
    }).state('store-info', {
        url: "/store-info",
        templateUrl: "templates/product/store-info.html",
        controller: 'productCtrl',
        params: { storeInfo: null }
    }).state('store-identy', {
        url: "/store-identy",
        cache: 'false',
        templateUrl: "templates/product/store-identy.html",
        controller: 'productCtrl',
        params: { storeInfo: null }
    }).state('product-activity', {
        url: "/product-activity",
        templateUrl: "templates/product-activity.html",
        controller: 'productCtrl'
    }).state('tab.teaHome', {
        url: '/teaHome',
        views: {
            'tab-home': {
                templateUrl: 'templates/teaHome/tea-home.html',
                controller: 'teaHomeCtrl',
                cache: 'false'
            }
        },
        cache: 'false'
    }).state('add-talk', {
        url: "/add-talk",
        cache: 'false',
        templateUrl: "templates/teaHome/add-talk.html",
        controller: 'teaHomeCtrl'
    }).state('store', {
        url: "/store",
        cache: 'false',
        templateUrl: "templates/product/store.html",
        controller: 'productCtrl',
        params: {
            store_id: ''
        }
    }).state('talk-comment', {
        url: "/talk-comment",
        cache: 'false',
        templateUrl: "templates/teaHome/talk-comment.html",
        controller: 'teaHomeCtrl',
        params: {
            blog_id: null,
            user_is_followed: false
        }
    }).state('share-talk', {
        url: "/share-talk",
        cache: 'false',
        templateUrl: "templates/teaHome/share-talk.html",
        controller: 'teaHomeCtrl',
        params: {
            blog_id: null
        }
    }).state('store-search', {
        url: "/store-search",
        templateUrl: "templates/product/store-search.html",
        controller: 'productCtrl',
        cache: 'false'
    }).state('store-underLine', {
        url: "/store-underLine",
        cache: 'false',
        templateUrl: "templates/product/store-underLine.html",
        controller: 'productCtrl',
        params: {
            storeInfo: null
        }
    }).state('product-class', {
        url: "/product-class",
        cache: 'false',
        templateUrl: "templates/product/product-class.html",
        controller: 'productCtrl',
        params: {
            classId: null,
            className:''
        }
    }).state('tea-shop', {
        url: "/tea-shop",
        templateUrl: "templates/teaHome/tea-shop.html",
        controller: 'teaHomeDetailCtrl',
        cache: false,
        params: {
            id: ''
        }
    }).state('tab.friends', {
        url: '/friends',
        views: {
            'tab-friends': {
                templateUrl: 'templates/tab-friends.html',
                controller: 'TeaFriendsCtrl'
            },
        },
        cache: false,
    }).state('friends_list', {
        url: "/friends/list",
        templateUrl: "templates/friends/friends_list.html",
        controller: 'teaFriendsList'
    }).state('friends_search', {
        url: "/friends/search",
        templateUrl: "templates/friends/friends_search.html",
        controller: 'teaFriendsList',

    }).state('chat', {
        url: "/friends/chat",
        templateUrl: "templates/friends/chat.html",
        controller: 'ChatCtrl',
        params: {
            id: ''
        },
        cache: false,
    }).state('group_list', {
        url: "/friends/group/list",
        templateUrl: "templates/friends/tea-group.html",
        controller: 'GroupCtrl',
        cache: false,
    }).state('chat_group', {
        url: "/friends/group/chat",
        templateUrl: "templates/friends/chat-group.html",
        controller: 'GroupChatCtrl',
        params: {
            id: ''
        }
    }).state('chat-group-detail', {
        url: "/friends/group/detail",
        templateUrl: "templates/friends/tea-group-detail-edit.html",
        controller: 'GroupChatCtrl',
        params: {
            id: ''
        },
        cache: false,
    }).state('create_group', {
        url: "/friends/group/create",
        templateUrl: "templates/friends/create-group.html",
        controller: 'GroupCtrl'
    }).state('tea_frends_rank', {
        url: '/friends/tea_friends_rank',
        // abstract: true,
        templateUrl: 'templates/friends/tea-friends-rank.html',
        controller: 'teaFriendsRankCtrl'
    }).state('recommend_tea_friends', {
        url: '/friends/recommend_tea_friends',
        templateUrl: 'templates/friends/recommend-tea-friends.html',
        controller: 'teaFriendsRecommendCtrl'
    }).state('store-list', {
        url: '/store-list',
        templateUrl: 'templates/main/store-list.html',
        controller: 'productCtrl',
        cache: 'false',
        params: {
        	isSearch:null,
        	store_list:null,
        	keyword:null
        }
    }).state('tab.account', {
        url: '/account',
        views: {
            'tab-account': {
                templateUrl: 'templates/tab-account.html',
                controller: 'accountCtrl'
            }
        },
        cache: 'false'
    }).state('other-account', {
        url: "/account/user",
        templateUrl: "templates/account/user-main.html",
        controller: 'otherAccountCtrl',
        cache: 'false',
        params: {
            id: ''
        }
    }).state('ad-detail', {
        url: "/ad/detail",
        templateUrl: "templates/notify/ad-detail.html",
        controller: 'notifyCtrl',
        params: {
            id: ''
        }
    }).state('account-setting', {
        url: "/account/setting",
        templateUrl: "templates/account/account-setting.html",
        controller: 'settingCtrl'
    }).state('account-sign', {
        url: "/account/sign",
        templateUrl: "templates/account/account-sign.html",
        controller: 'settingCtrl'
    }).state('setting-personal', {
        url: "/account/setting/personal",
        templateUrl: "templates/account/setting-personal.html",
        controller: 'settingPersonalCtrl',
        cache: 'false'
    }).state('setting-name', {
        url: "/account/setting/name",
        templateUrl: "templates/account/setting-name.html",
        controller: 'settingCtrl'
    }).state('setting-address', {
        url: "/account/setting/address",
        templateUrl: "templates/account/setting-address.html",
        controller: 'settingCtrl',
        params: {
            isChooseAddress: false,
            orderInfo: {},
            from_url: ''
        },
        cache: 'false'
    }).state('setting-phone', {
        url: "/account/setting/phone",
        templateUrl: "templates/account/setting-phone.html",
        controller: 'settingCtrl'
    }).state('setting-gender', {
        url: "/account/setting/gender",
        templateUrl: "templates/account/setting-gender.html",
        controller: 'settingCtrl'
    }).state('setting-area', {
        url: "/account/setting/area",
        templateUrl: "templates/account/setting-area.html",
        controller: 'settingCtrl'
    }).state('setting-area-simple', {
        url: "/account/setting/area/simple",
        templateUrl: "templates/account/setting-area-simple.html",
        controller: 'settingCtrl'
    }).state('setting-sign', {
        url: "/account/setting/sign",
        templateUrl: "templates/account/setting-sign.html",
        controller: 'settingCtrl'
    }).state('setting-headImg', {
        url: "/account/setting/headImg",
        templateUrl: "templates/account/setting-headImg.html",
        controller: 'accountCtrl'
    }).state('setting-address-add', {
        url: "/account/setting/address-add",
        templateUrl: "templates/account/setting-address-add.html",
        controller: 'settingAddressCtrl',
        params: {
            editAddress: null,
            isChooseAddress: false
        }
    }).state('setting-about', {
        url: "/account/setting/about",
        templateUrl: "templates/account/setting-about.html",
        controller: 'settingCtrl'
    }).state('setting-question', {
        url: "/account/setting/question",
        templateUrl: "templates/account/setting-question.html",
        controller: 'settingCtrl'
    }).state('account-login', {
        url: "/account/user/login",
        templateUrl: "templates/account/login.html",
        controller: 'loginCtrl'
    }).state('account-register', {
        url: "/account/user/register",
        templateUrl: "templates/account/register.html",
        controller: 'registerCtrl'
    }).state('account-find-psw-back', {
        url: "/account/user/psw/back",
        templateUrl: "templates/account/find-psw-back.html",
        controller: 'registerCtrl'
    }).state('account-my-collection', {
        url: "/account/user/collection",
        templateUrl: "templates/account/my-collection.html",
        controller: 'accountCollectionCtrl'
    }).state('account-tea-b-exchange', {
        url: "/account/teab/exchange",
        templateUrl: "templates/account/tea-b-exchange.html",
        controller: 'teaBCtrl'
    }).state('account-shop-cart', {
        url: "/account/shop/cart",
        templateUrl: "templates/account/shop-cart.html",
        controller: 'shopCartCtrl',
        params: {
            userInfo: null
        },
        cache: false
    }).state('account-shop-cart-wuliu', {
        url: "/account/shop/cart/wuliu",
        templateUrl: "templates/account/shop-cart-wuliu.html",
        controller: 'wuliuCtrl',
        params: {
            item: null
        },
        cache: 'false'
    }).state('account-shop-cart-no-transport', {
        url: "/account/shop/cart/no/transport",
        templateUrl: "templates/account/shop-cart-no-transport.html",
        controller: 'shopCartCtrl'
    }).state('account-shop-cart-no-pay', {
        url: "/account/shop/cart/no/pay",
        templateUrl: "templates/account/shop-cart-no-pay.html",
        controller: 'shopCartCtrl',
        cache: false
    }).state('account-shop-cart-wuliu-list', {
        url: "/account/shop/cart/wuliu/list",
        templateUrl: "templates/account/shop-cart-wuliu-list.html",
        controller: 'shopCartCtrl'
    }).state('account-shop-cart-history', {
        url: "/account/shop/cart/history",
        templateUrl: "templates/account/shop-cart-history.html",
        controller: 'shopCartCtrl'
    }).state('account-msg', {
        url: "/account/msg",
        templateUrl: "templates/account/account-msg.html",
        controller: 'TeaFriendsCtrl',
        cache: false
    }).state('account-point-exchange', {
        url: "/account/point/exchange",
        templateUrl: "templates/account/point-exchange.html",
        controller: 'teaBCtrl'
    }).state('account-point-exchange-successful', {
        url: "/account/point/exchange/successful",
        templateUrl: "templates/account/point-exchange-successful.html",
        controller: 'teaBCtrl'
    }).state('account-tea-b-exchange-successful-list', {
        url: "/account/teab/exchange/successful/list",
        templateUrl: "templates/account/tea-b-exchange-success-list.html",
        controller: 'teaBCtrl'
    }).state('account-tea-b-pai-dialog', {
        url: "/account/teab/pai/dialog",
        templateUrl: "templates/account/dialog/rush-pai-dialog.html",
        controller: 'teaBCtrl'
    }).state('account-tea-b-task-detail', {
        url: "/account/teab/task/detail",
        templateUrl: "templates/account/tea-b-task-detail.html",
        controller: 'teaBCtrl'
    });

    // if none of the above states are matched, use this as the fallback
    $urlRouterProvider.otherwise('/tab/main');
});
